package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.aicms.*;
import com.dd.cloud.user.req.aicms.*;
import com.dd.cloud.user.res.aicms.*;
import com.dd.cloud.user.service.aicms.*;
import com.dd.cloud.user.vo.DiagnosticRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 医生账号信息表  前端控制器
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Validated
@RestController
@RequestMapping("/ai")
public class AicmsController {
    @Autowired
    private IAicmsDepartmentService aicmsDepartmentService;
    @Autowired
    private IAicmsDiseaseService aicmsDiseaseService;
    @Autowired
    private IAicmsSymptomService aicmsSymptomService;
    @Autowired
    private IAicmsDoctorService aicmsDoctorService;
    @Autowired
    private IAicmsDiagnosticResultService aicmsDiagnosticResultService;

    /**
     * 查询所有科室
     *
     * @return 科室列表
     */
    @GetMapping("/key/info")
    public String keyInfo() {
        return aicmsDepartmentService.keyInfo();
    }


    /**
     * 查询所有科室
     *
     * @return 科室列表
     */
    @GetMapping("/dep/list")
    public ResponseEntity<List<AicmsDepartment>> queryDepartMents(String sex, String age) {
        List<AicmsDepartment> deps = aicmsDepartmentService.queryDepartMents(sex, age);
        return ResponseEntity.ok(deps);
    }

    /**
     * 查询所有科室 只能查出后台配置的科室
     *
     * @return 科室列表
     */
    @GetMapping("/dep/config")
    public ResponseEntity<List<AicmsDepartment>> queryConfigDepartMents() {
        List<AicmsDepartment> deps = aicmsDepartmentService.queryConfigDepartMents();
        return ResponseEntity.ok(deps);
    }

    /**
     * 查询所有科室
     *
     * @return 科室列表
     */
//    @Async
    @GetMapping("/dep/dis/all")
    public ResponseEntity<List<AllDdpRes>> queryDepDisAll() {
        List<AllDdpRes> all = aicmsDepartmentService.getAllDepDis();
        return ResponseEntity.ok(all);
    }

    /**
     * 根据科室ID查询疾病
     *
     * @param depId 科室ID
     * @return 疾病列表
     */
    @GetMapping("/dise")
    public ResponseEntity<List<AicmsDisease>> queryDiseases(@NotEmpty(message = "科室id不能为空") String depId, String sex, String age) {
        List<AicmsDisease> diseases = aicmsDiseaseService.queryDiseases(depId, sex, age);
        return ResponseEntity.ok(diseases);
    }


    /**
     * 根据疾病ID查询子级疾病
     *
     * @param jibingId 疾病ID
     * @return 疾病列表
     */
    @GetMapping("/subdise")
    public ResponseEntity<List<AicmsDisease>> querySubDiseases(@NotEmpty(message = "疾病id不能为空") String jibingId, String sex, String age) {
        List<AicmsDisease> diseases = aicmsDiseaseService.querySubDiseases(jibingId, sex, age);
        return ResponseEntity.ok(diseases);
    }


    /**
     * 根据疾病ID查询专家信息
     *
     * @param jibingId 疾病ID
     * @return 专家信息
     * 该接口存在问题，需要改为通过疾病序号查询专家信息，记录日期20200222
     */
    @GetMapping("/doctor")
    public ResponseEntity<AicmsDoctor> queryDoctor(@NotEmpty(message = "疾病id不能为空") String jibingId) {
        AicmsDoctor doc = aicmsDoctorService.queryDoctor(jibingId);
        return ResponseEntity.ok(doc);
    }

    /**
     * 根据疾病序号查询专家信息
     *
     * @param sXuHao 疾病序号
     * @return 专家信息
     */
    @GetMapping("/doctor/xuhao")
    public ResponseEntity<AicmsDoctor> queryDoctorByXuhao(@NotEmpty(message = "疾病序号不能为空") String sXuHao) {
        AicmsDoctor doc = aicmsDoctorService.queryDoctorByXuhao(sXuHao);
        return ResponseEntity.ok(doc);
    }

    /**
     * 根据疾病序号查询症状
     *
     * @param xuHao 疾病序号
     * @return 症状列表
     */
    @GetMapping("/symptom")
    public ResponseEntity<List<AicmsSymptom>> querySymptoms(@NotEmpty(message = "疾病序号不能为空") String xuHao) {
        List<AicmsSymptom> symptoms = aicmsSymptomService.querySymptoms(xuHao);
        return ResponseEntity.ok(symptoms);
    }

    /**
     * 症状模糊查询首字母
     */
    @GetMapping("/symptom/keyword")
    public ResponseEntity<List<AicmsSymptom>> querySymptomsKeyword(@NotEmpty(message = "症状不能为空") String keyword,
                                                                   @NotEmpty(message = "疾病序号不能为空") String xuHao) {
        List<AicmsSymptom> symptoms = aicmsSymptomService.querySymptomsKeyword(keyword, xuHao);
        return ResponseEntity.ok(symptoms);
    }

    /**
     * 根据关键字查询科室疾病
     *
     * @param keyword 搜索关键字
     * @return 症状列表
     */
    @GetMapping("/search/keyword")
    public ResponseEntity<List<AicmsDisease>> search(@NotEmpty(message = "关键字不能为空") String keyword) {
        List<AicmsDisease> symptoms = aicmsDiseaseService.search(keyword);
        return ResponseEntity.ok(symptoms);
    }

    /**
     * 根据关键字查询科室一级疾病 二级疾病,症状
     *
     * @param keyword    搜索关键字
     * @param depId
     * @param timestamp
     * @param nHaveChild 传0为最终疾病
     * @return 症状列表
     */
    @GetMapping("/search/all")
    public ResponseEntity<List<SearchRes>> searchAll(@NotEmpty(message = "搜索关键字不能为空") String keyword, String timestamp, String depId, Integer nHaveChild) {
        List<SearchRes> symptoms = aicmsDiseaseService.searchAll(keyword, timestamp, depId, nHaveChild);
        return ResponseEntity.ok(symptoms);
    }

    /**
     * 查询科室和最终疾病
     *
     * @param req
     * @return
     */
    @GetMapping("/search/dep/find/disease")
    public ResponseEntity<IPage<DepAndFindDiseaseRes>> queryDepAndFindDisease(@Valid DepAndFindDiseaseReq req) {
        IPage<DepAndFindDiseaseRes> res = aicmsDiseaseService.queryDepAndFindDisease(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 在线诊断接口
     *
     * @param req 诊断参数
     * @return 诊断结果
     */
    @PostMapping("/ask")
    public ResponseEntity<DiagnosticRecordVo> ask(HttpServletRequest request, @Valid AskDoctorParamsReq req) {
        DiagnosticRecordVo record = aicmsDiagnosticResultService.ask(request, req);
        return ResponseEntity.ok(record);
    }

    /**
     * 根据诊断记录编号查询诊断记录
     *
     * @param tencode 断记录编号
     * @return 诊断记录
     */
    @GetMapping("/diagnos")
    public ResponseEntity<AicmsDiagnosticResult> queryDiagnosticsByTencode(@NotEmpty(message = "诊断编号不能为空") String tencode) {
        AicmsDiagnosticResult records = aicmsDiagnosticResultService.queryDiagnosticsByTencode(tencode);
        return ResponseEntity.ok(records);
    }

    /**
     * 根据患者ID查询诊断记录
     *
     * @param req 就诊卡ID
     * @return 诊断记录
     */
    @GetMapping("/diagnostics/patient")
    public ResponseEntity<Page<AicmsDiagnosticMedPageRes>> queryDiagnosticsByPatient(@Valid PatientRecordReq req) {
        Page<AicmsDiagnosticMedPageRes> records = aicmsDiagnosticResultService.queryDiagnosticsByPatient(req);
        return ResponseEntity.ok(records);
    }

    /**
     * 查询诊断记录 多条件查询
     *
     * @param req
     * @return 诊断记录
     */
    @GetMapping("/diagnostics/terms")
    public ResponseEntity<IPage<AicmsDiagnosticResultPageRes>> queryDiagnosticsTerms(@Valid DiagnosticRecordReq req) {
        IPage<AicmsDiagnosticResultPageRes> iPage = aicmsDiagnosticResultService.queryDiagnosticsTerms(req);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 查询就诊人的诊断记录
     *
     * @param req
     * @return 诊断记录
     */
    @GetMapping("/diagnostics/page/med")
    public ResponseEntity<Page<AicmsDiagnosticMedPageRes>> queryDiagnosticsTerms(@Valid DiagnosticRecordMedReq req) {
        Page<AicmsDiagnosticMedPageRes> iPage = aicmsDiagnosticResultService.queryDiagnosticsMedTerms(req);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 删除诊断记录
     *
     * @param tencode
     */
    @PostMapping("/diagnostics/del")
    public ResponseEntity<Void> delDiagnostics(@NotEmpty(message = "诊断编号不能为空") String tencode) {
        aicmsDiagnosticResultService.delDiagnostics(tencode);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询诊断记录 运营后台无效订单列表
     *
     * @param req
     * @return
     */
    @GetMapping("/diagnostics/page/system")
    public ResponseEntity<IPage<AicmsDiagnosticResultSystemPageRes>> queryAicmsDiagnosticResultSystemPage(@Valid AicmsDiagnosticResultSystemPageReq req) {
        IPage<AicmsDiagnosticResultSystemPageRes> res = aicmsDiagnosticResultService.queryAicmsDiagnosticResultSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 导出运营后台无效订单列表
     *
     * @param response
     * @param req
     */
    @GetMapping("/diagnostics/page/system/excel")
    public ResponseEntity<Void> exportAicmsDiagnosticResultSystemExcel(HttpServletResponse response, AicmsDiagnosticResultSystemPageReq req) {
        aicmsDiagnosticResultService.exportAicmsDiagnosticResultSystemExcel(response, req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询诊断记录详情 运营后台无效订单列表
     *
     * @param diagnosticId 诊断id
     * @return
     */
    @GetMapping("/diagnostics/detail/system")
    public ResponseEntity<AicmsDiagnosticResultSystemDetailRes> queryAicmsDiagnosticResultSystemDetailRes(@NotNull(message = "诊断id不能为空") Integer diagnosticId) {
        AicmsDiagnosticResultSystemDetailRes res = aicmsDiagnosticResultService.queryAicmsDiagnosticResultSystemDetailRes(diagnosticId);
        return ResponseEntity.ok(res);
    }

    /**
     * 导入症状ai口语化表达的标志位
     *
     * @return
     */
    @PostMapping("/symptom/import/ai")
    public ResponseEntity<Void> symptomImportAi(@NotNull(message = "请上传文件") MultipartFile file) {
        aicmsSymptomService.symptomImportAi(file);
        return ResponseEntity.ok().build();
    }
}

